package cn.chousensei.chousenseiblog.controller;

import cn.chousensei.chousenseiblog.cache.TagCache;
import cn.chousensei.chousenseiblog.enums.PublishErrorEnum;
import cn.chousensei.chousenseiblog.model.Blog;
import cn.chousensei.chousenseiblog.model.User;
import cn.chousensei.chousenseiblog.service.BlogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class PublishController {

    @Autowired
    BlogService blogService;

    @GetMapping("/publish")
    public String publish(Model model) {
        model.addAttribute("tags", TagCache.get());
        return "publish";
    }

    @PostMapping("/publish")
    public String doPublish(@RequestParam(name = "title", required = false) String title,
                            @RequestParam(name = "content", required = false) String content,
                            @RequestParam(name = "tag", required = false) String tag,
                            @RequestParam(name = "id", required = false) Long id,
                            @RequestParam(name = "type", required = false) Integer type,
                            HttpServletRequest request,
                            Model model) {

        model.addAttribute("title", title);
        model.addAttribute("content", content);
        model.addAttribute("tag", tag);
        model.addAttribute("tags", TagCache.get());

        if (StringUtils.isBlank(title)) {
            model.addAttribute("error", PublishErrorEnum.TITLE_EMPTY.getMessage());
            model.addAttribute("id", id);
            return "publish";
        }
        if (StringUtils.isBlank(content)) {
            model.addAttribute("error", PublishErrorEnum.CONTENT_EMPTY.getMessage());
            model.addAttribute("id", id);
            return "publish";
        }
        if (type == null) {
            model.addAttribute("error", PublishErrorEnum.TYPE_EMPTY.getMessage());
            model.addAttribute("id", id);
            return "publish";
        }
        if (StringUtils.isBlank(tag)) {
            model.addAttribute("error", PublishErrorEnum.TAG_EMPTY.getMessage());
            model.addAttribute("id", id);
            return "publish";
        }
        String invalid = TagCache.filterInvalid(tag);
        if (StringUtils.isNotBlank(invalid)) {
            model.addAttribute("error", PublishErrorEnum.TAG_ILLEGAL_INPUT.getMessage() + invalid);
            model.addAttribute("id", id);
            return "publish";
        }
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            model.addAttribute("error", PublishErrorEnum.USER_NOT_LOGIN.getMessage());
            model.addAttribute("id", id);
            return "publish";
        }

        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setTag(tag);
        blog.setType(type);
        blog.setId(id);
        blog.setCreator(user.getUserId());

        try {
            blogService.createOrUpdate(blog);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/";
    }


}
